package fun.cyhgraph.controller.admin;

import fun.cyhgraph.annotation.AuthCheck;
import fun.cyhgraph.common.UserConstant;
import fun.cyhgraph.model.dto.OrderCancelDTO;
import fun.cyhgraph.model.dto.OrderConfirmDTO;
import fun.cyhgraph.model.dto.OrderPageDTO;
import fun.cyhgraph.model.dto.OrderRejectionDTO;
import fun.cyhgraph.result.PageResult;
import fun.cyhgraph.result.Result;
import fun.cyhgraph.service.OrderService;
import fun.cyhgraph.model.vo.OrderStatisticsVO;
import fun.cyhgraph.model.vo.OrderVO;
import fun.cyhgraph.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Tag(name = "订单管理")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;

    /**
     * 分页查询所有订单
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "分页查询所有订单(根据merchant判断是否查询全部商家(null查询所有))")
        @PostMapping("/pageOrder")
    public Result<PageResult> pageOrder(@RequestBody OrderPageDTO orderPageDTO , HttpServletRequest request){
        PageResult pageResult = orderService.conditionSearch(orderPageDTO , request);
        return Result.success(pageResult);
    }
    /**
     * 不同状态订单数量统计
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "不同状态订单数量统计(不加商铺id，默认查询所有订单)")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics(@RequestParam(required = false) Integer merchantId , HttpServletRequest request){
        if (merchantId == null){
            Integer userId = userService.getLoginUser(request).getId();
            OrderStatisticsVO orderStatisticsVO = orderService.statistics(userId);
            return Result.success(orderStatisticsVO);
        }else {
            OrderStatisticsVO orderStatisticsVO = orderService.statistics();
            return Result.success(orderStatisticsVO);
        }


    }

    /**
     * 根据订单id查询订单信息
     * @param id
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "根据订单id查询订单信息")
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable Integer id , @RequestParam(required = false) Integer merchantId , HttpServletRequest request){
        if (merchantId == null){
            OrderVO orderVO = orderService.getById(id);
            return Result.success(orderVO);
        }
        OrderVO orderVO = orderService.getById1(id , merchantId);
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param orderConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrderConfirmDTO orderConfirmDTO){
        log.info("修改订单状态为接单：{}", orderConfirmDTO);
        orderService.confirm(orderConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     * @param orderRejectionDTO
     * @return
     */
    @PutMapping("/reject")
    public Result reject(@RequestBody OrderRejectionDTO orderRejectionDTO){
        log.info("拒单，有原因：{}", orderRejectionDTO);
        orderService.reject(orderRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @param orderCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrderCancelDTO orderCancelDTO){
        log.info("取消订单，有原因：{}", orderCancelDTO);
        orderService.cancel(orderCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Integer id){
        log.info("派送中：{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Integer id){
        log.info("已完成：{}", id);
        orderService.complete(id);
        return Result.success();
    }

}
